/**
 * 换算文件大小单位
 * Approximate to the closest prefixed unit
 *
 * @param {number} numberOfBytes 以字节为单位的文件大小
 * @returns
 */
function transformFileSizeUnit(numberOfBytes) {
  const units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
  const exponent = Math.min(Math.floor(Math.log(numberOfBytes) / Math.log(1024)), units.length - 1);
  const approx = numberOfBytes / 1024 ** exponent;
  const output =
    exponent === 0 ? `${numberOfBytes} bytes` : `${approx.toFixed(3)} ${units[exponent]} (${numberOfBytes} bytes)`;
  return output;
}

console.log(transformFileSizeUnit(1024));
